﻿Public Class dlgLineSources

    Private Sub txtWidth_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtWidth.Validating
        Dim i As Integer
        For i = 0 To Me.ListPolute.SelectedItems.Count - 1
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).Width = Me.txtWidth.Value
        Next
    End Sub

    Private Sub txtRelhgt_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRelhgt.Validating
        Dim i As Integer
        For i = 0 To Me.ListPolute.SelectedItems.Count - 1
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).Relhgt = Me.txtRelhgt.Value
        Next
    End Sub

    Private Sub txtLineCounts_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLineCounts.Validating
        Me.EFlexNVerts.Rows.Count = Me.txtLineCounts.Value + 2
        For i As Integer = 1 To Me.txtLineCounts.Value
            EFlexNVerts.SetData(i, 0, i)
        Next
        EFlexNVerts_Validating(sender, e)
    End Sub

    Private Sub EFlexNVerts_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles EFlexNVerts.AfterEdit
        txtXs.Value = Me.EFlexNVerts.GetData(1, 1)
        txtYs.Value = Me.EFlexNVerts.GetData(1, 2)
    End Sub

    Private Sub EFlexNVerts_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EFlexNVerts.Validating
        '设置第一个点
        Dim i As Integer
        For i = 0 To Me.ListPolute.SelectedItems.Count - 1
            '设置顶点
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).Location.Xs = Me.EFlexNVerts.GetData(1, 1)
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).Location.Ys = Me.EFlexNVerts.GetData(1, 2)
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).Location.Zs = Me.EFlexNVerts.GetData(1, 3)

            '设置其它顶点
            ReDim CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).LinePoints(Me.EFlexNVerts.Rows.Count - 2)
            For j As Integer = 0 To Me.EFlexNVerts.Rows.Count - 2
                CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).LinePoints(j) = New CAermod.Point3D
                CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).LinePoints(j).x = Me.EFlexNVerts.GetData(j + 1, 1)
                CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).LinePoints(j).y = Me.EFlexNVerts.GetData(j + 1, 2)
                CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).LinePoints(j).z = Me.EFlexNVerts.GetData(j + 1, 3)
            Next
        Next
    End Sub

    Private Sub ListPolute_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListPolute.SelectedIndexChanged
        If Me.ListPolute.SelectedIndex >= 0 Then
            Me.txtWidth.Value = CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).Width
            Me.txtRelhgt.Value = CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).Relhgt
            Me.txtHeight.Value = CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).Height
            Me.txtLineCounts.Value = CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).LinePoints.Length - 1  '边数为顶点数-1
            Me.EFlexNVerts.Rows.Count = Me.txtLineCounts.Value + 2 '表格行数等于边数+2
            '设置第一个点
            Me.EFlexNVerts.SetData(1, 1, CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).Location.Xs)
            Me.EFlexNVerts.SetData(1, 2, CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).Location.Ys)
            Me.EFlexNVerts.SetData(1, 3, CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).Location.Zs)
            '设置所有顶点
            For n As Integer = 0 To Me.txtLineCounts.Value
                Me.EFlexNVerts.SetData(n + 1, 1, CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).LinePoints(n).x)
                Me.EFlexNVerts.SetData(n + 1, 2, CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).LinePoints(n).y)
                Me.EFlexNVerts.SetData(n + 1, 3, CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).LinePoints(n).z)

            Next
            '设置顶点序号
            For i As Integer = 1 To Me.txtLineCounts.Value + 1
                EFlexNVerts.SetData(i, 0, i)
            Next
            Me.cmbPolluteUnit.Text = CType(Me.BaseSoruce(ListPolute.SelectedIndex), CAermod.Line).PolluteUnit
        End If
    End Sub

    Private Sub dlgAeraPolySource_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.EFlexEmisFact.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlexMethod1.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlexNVerts.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlexSourceNameAndS.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)

        Me.txtXs.Enabled = False
        Me.txtYs.Enabled = False
        Me.txtZs.Enabled = False
        Me.EFlexSourceNameAndS.SetData(0, 1, "排放速率")

        Me.EFlexEmisFact.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexEmisFact.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexEmisFact.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.EFlexMethod1.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexMethod1.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexMethod1.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.EFlexNVerts.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexNVerts.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexNVerts.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.EFlexSourceNameAndS.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexSourceNameAndS.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexSourceNameAndS.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None
    End Sub

    
    Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

        Dim LineSource(-1) As CAermod.Line
        LineSource = Me.m_BaseSoruce
        ReDim Preserve LineSource(LineSource.Length)
        LineSource(LineSource.Length - 1) = New CAermod.Line
        ReDim LineSource(LineSource.Length - 1).PollutionPara(Me.m_ArrayPollut.Length - 1) '设置新增加的污染源的污染物的类型
        LineSource(LineSource.Length - 1).PollutionPara(Me.m_ArrayPollut.Length - 1) = New CAermod.PollutionPara
        LineSource(LineSource.Length - 1).SetPollutePara(AermodProject0.Aermod.Control.AllPolluts)
        LineSource(LineSource.Length - 1).Location.Srcid = "L" & (Me.m_SourceCount + 1)
        Me.m_SourceCount += 1
        LineSource(LineSource.Length - 1).SourceName = "线源" & m_SourceCount '污染源名称
        Me.m_BaseSoruce = LineSource
        ''修改列表
        Me.ListPolute.Items.Add(LineSource(LineSource.Length - 1).SourceName)

        ListPolute.SelectedIndex = ListPolute.Items.Count - 1
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        '根据用户的设置创建线源数组
        
        For i As Integer = 0 To CType(Me.BaseSoruce, CAermod.Line()).Length - 1
            CType(Me.BaseSoruce(i), CAermod.Line).CreateVolumeLocation()
        Next
    End Sub

    Private Sub txtHeight_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtHeight.Validating
        Dim i As Integer
        For i = 0 To Me.ListPolute.SelectedItems.Count - 1
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).Height = Me.txtHeight.Value
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel.Click
        Dim iSelected As Integer = ListPolute.SelectedIndex
        If iSelected >= 0 Then
            Dim Lines(-1) As CAermod.Line
            Lines = Me.m_BaseSoruce
            If iSelected <= ListPolute.Items.Count - 2 Then
                For i As Integer = iSelected To ListPolute.Items.Count - 2
                    Lines(i) = Lines(i + 1)

                Next
            End If
            ReDim Preserve Lines(Lines.Length - 2)
            ListPolute.Items.Clear()
            For i As Integer = 0 To Lines.Length - 1
                ListPolute.Items.Add(Lines(i).SourceName)
            Next
            If ListPolute.Items.Count > 0 Then
                If iSelected <= ListPolute.Items.Count - 1 Then
                    ListPolute.SelectedIndex = iSelected
                Else
                    ListPolute.SelectedIndex = iSelected - 1
                End If
            End If

            Me.m_BaseSoruce = Lines
        End If
    End Sub

    Private Sub cmbPolluteUnit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPolluteUnit.SelectedIndexChanged
        Dim i As Integer
        For i = 0 To Me.ListPolute.SelectedItems.Count - 1
            CType(Me.BaseSoruce(ListPolute.SelectedIndices(i)), CAermod.Line).PolluteUnit = Me.cmbPolluteUnit.Text
        Next
    End Sub
    Public Sub Modify()
        Dim p As New Point
        p.X = Me.Location.X
        p.Y = Me.UltraTabControl3.Location.Y
        Me.UltraTabControl3.Location = p
        Me.cmdAdd.Visible = False
        Me.cmdDel.Visible = False
        Me.Width = 500
    End Sub

    Private Sub txtLineCounts_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLineCounts.TextChanged

    End Sub
End Class
